package cqrtplm.listener;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.SrmIntegrityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

import java.math.BigInteger;

@CustomListener(
        category = "润通",
        description = "图纸下发SRM"
)
@Slf4j
public class SendDrawingToSRMListener implements ExecutionListener {
    @CustomListenerField(description = "类型",defaultValue ="",required = true)
    private FixedValue parma;
    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("----执行监听润通定制-图纸下发SRM 开始----");
        long l = System.currentTimeMillis();
        String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = delegateExecution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        log.info("流程监听，图纸下发SRM,id:{}", workFlowContainerId);
        SrmIntegrityService srmIntegrityService = SpringUtil.getBean(SrmIntegrityService.class);
//        Map<String, Object> requestContext = RequestContext.currentContext();
//        CompletableFuture.runAsync(() -> {
//            RequestContext.attachContext(requestContext);
            try {
                srmIntegrityService.sendDrawingToSrm(new BigInteger(workFlowContainerId),parma.getExpressionText());
            } catch (Exception e) {
                log.info("图纸下发SRM");
                log.error(ExceptionUtil.getMessage(e));
                log.error(ExceptionUtil.stacktraceToString(e, 100000));
                e.printStackTrace();
            }
//        });
        log.info("----执行监听润通定制-图纸下发SRM 结束，耗时:{}----", System.currentTimeMillis() - l);
    }



}
